package com.example.demo.controller;

import com.example.demo.pojo.DTO.UserDTO;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.entity.User;
import com.example.demo.service.IUserService;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user") //localhost:8080/user 访问
@Slf4j
public class UserController {

    @Autowired
    IUserService userService;

    /**
     * 插入用户
     *
     * @param userDTO
     * @return
     */
    @PostMapping
    public Result<UserDTO> add(@RequestBody UserDTO userDTO){
        log.info("增加功能测试：{}",userDTO);
        userService.add(userDTO);
        return Result.success(userDTO);
    }

    //删
    @DeleteMapping("/{user_id}")
    public Result delete(@PathVariable Integer user_id){
        userService.delete(user_id);
        return Result.success();
    }
    //改
    @PutMapping
    public Result<User> edit(@RequestBody UserDTO userDTO){
        User userResult = userService.edit(userDTO);
        return Result.success(userResult);
    }
    //查
    @GetMapping("/{user_id}")
    public Result<User> get(@PathVariable Integer user_id){
        User user = userService.get(user_id);
        return Result.success(user);
    }
}
